package ru.rchervot.sanasar.client.view;

import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.widgetideas.client.GlassPanel;

public abstract class AbstractSearchBoxView extends Composite implements SearchBoxPresenter.Display {
	
	private Presenter presenter;
	
	private final HorizontalPanel basePanel = new HorizontalPanel();
	private final Button searchButton = new Button("Search");
	private final Button filtersButton = new Button("Advanced Search");
	private final TextBox searchField = new TextBox();
	private final DialogBox dialogBox = new DialogBox();
	
	public AbstractSearchBoxView(Widget filtersPanel) {
		super();
				
		basePanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		basePanel.add(searchField);
		basePanel.add(searchButton);
		basePanel.add(filtersButton);
		
		dialogBox.add(filtersPanel);
		dialogBox.setHTML("<b>Advanced Search</b>");
		basePanel.setStylePrimaryName("AbstractSearchBoxView");
		this.initWidget(basePanel);
				
		searchField.addKeyPressHandler(new KeyPressHandler() {
      public void onKeyPress(KeyPressEvent event) {
      	if ((event.getCharCode() == 13) && (! event.isAnyModifierKeyDown())) {
      		searchButton.click();
      	}
      }
    });
	}
	
	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}
	
	@Override
	public HasClickHandlers getFiltersButton() {
		return filtersButton;
	}

	@Override
	public HasClickHandlers getSearchButton() {
		return searchButton;
	}

	@Override
	public HasValue<String> getSearchField() {
		return searchField;
	}
	
	@Override
	public void setVisibleFilters(boolean visible) {
		if (visible) {
			dialogBoxShow();
		} else {
			dialogBoxHide();
		}
	}
	
	private void dialogBoxShow() {
		addGlassPanel();
		dialogBox.setPopupPositionAndShow(new PopupPanel.PositionCallback() {
			public void setPosition(int offsetWidth,
					int offsetHeight) {
				int left = (Window.getClientWidth() - offsetWidth) / 2;
				int top = 0;
				dialogBox.setPopupPosition(left, top);
			}
		});
	}
	
	private void dialogBoxHide() {
		removeGlassPanel();
		dialogBox.hide();
	}
	
	private GlassPanel glassPanel;
	
	private void removeGlassPanel() {
    if (glassPanel != null) {
      glassPanel.removeFromParent();
    }
  }
	
	private void addGlassPanel() {
    removeGlassPanel();

    glassPanel = new GlassPanel(true);
    RootPanel.get().add(glassPanel, 0, 0);
  }
	
}
